using System;
using System.Collections.Generic;
using System.Text;
using TTDotNet.Util;
using TTDotNet.Engine.Networks;

namespace TTDotNet.Engine.Vehicles
{
    public class Fleet
    {
        //private List<Vehicle> roadVehicles;
        private List<Vehicle> trainVehicles;
        private List<Vehicle> airVehicles;
        private List<Vehicle> seaVehicles;

        private Dictionary<Guid, RoadVehicle> roadVehicles;

        public Fleet()
        {
            this.roadVehicles = new Dictionary<Guid, RoadVehicle>();
            //this.trainVehicles = new List<Vehicle>();
            //this.airVehicles = new List<Vehicle>();
            //this.seaVehicles = new List<Vehicle>();
        }

        public void Add(Vehicle vehicle, VehicleType type) {
            if (type == VehicleType.Bus || type == VehicleType.Lorry)
            {
                roadVehicles[vehicle.IMesh.GUID] = vehicle as RoadVehicle;
            }
            //else if (type == VehicleType.Rail)
            //{
            //    trainVehicles.Add(vehicle);
            //}
            //else if (type == VehicleType.Air)
            //{
            //    airVehicles.Add(vehicle);
            //}
            //else { seaVehicles.Add(vehicle); }
        }

        public Vehicle GetVehicle(Guid guid)
        {
            if (roadVehicles.ContainsKey(guid)) return roadVehicles[guid];

            return null;
        }

        private void MoveVehicles(DDDRoadNetwork rn, float fractionElapsed)
        {
            try
            {
                foreach (RoadVehicle v in roadVehicles.Values)
                {
                    if (v.NeedNewInterimDestination && !v.IsStopped)
                    {
                        RoadPath newPath = rn.AStarPathFind(v.ObjectDDDCoordinate, v.GetFinalDestination());
                        
                        //if(newPath != null)
                            v.SetNewRoadPath(newPath);
                    }

                    v.DoMove(fractionElapsed);
                }
            }
            catch (Exception e)
            {
                TTDotNet.Debug.GameLogger crashReport = new TTDotNet.Debug.GameLogger(e);
                Environment.Exit(1);
            }
        }

        private void PayVehicleMaintenance(TimeSpan timePassed)
        {
            float daysInYear = 365;
            if(DateTime.IsLeapYear(Game.gameWorld.CurrentDate.Year)) daysInYear = 366;

            float fractionOfYear = (float)timePassed.TotalDays / daysInYear;

            foreach (RoadVehicle v in roadVehicles.Values)
            {
                Game.gameWorld.GetCompany(Game.playerNumber).RemoveFunds(v.VehicleInformation.MaintenanceCost * (decimal)fractionOfYear, true);
            }
        }

        public void Update(float elapsedRealTime, TimeSpan elapsedGameTime)
        {
            MoveVehicles(World.roadNetwork, elapsedRealTime);
            PayVehicleMaintenance(elapsedGameTime);

        }

        public IEnumerator<Vehicle> GetEnumerator()
        {
            foreach (RoadVehicle r in roadVehicles.Values)
            {
                yield return r;
            }

            //foreach (Vehicle t in trainVehicles)
            //{
            //    yield return t;
            //}

            //foreach (Vehicle a in airVehicles)
            //{
            //    yield return a;
            //}

            //foreach (Vehicle s in seaVehicles)
            //{
            //    yield return s;
            //}
        }

    }
}
